package com.medata.utils;

import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author ：zb
 * @date ：Created in 2019/11/22 0022 9:18
 * @description：
 */
public class Jpg2WordUtils {

    public static void getWord(List<String> filepaths,String tempfile,String filename,HttpServletResponse response){
        XWPFDocument doc=new XWPFDocument();
        XWPFParagraph title=doc.createParagraph();
        XWPFRun run = title.createRun();
        for (String path:filepaths){
            try {
                FileInputStream is=new FileInputStream(new File(path));
                run.addBreak();
                run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, path, Units.toEMU(400), Units.toEMU(400)); // 200x200 pixels
                is.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        try {
            FileOutputStream fos=new FileOutputStream(tempfile);
            doc.write(fos);
            fos.close();
            FileInputStream is=new FileInputStream(new File(tempfile));
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + filename);
//            response.setContentType("application/msword");
            ServletOutputStream os = response.getOutputStream();
            IOUtils.copy(is,os);
            os.close();
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public static void main(String[] args) throws Exception{
        XWPFDocument doc = new XWPFDocument();

        XWPFParagraph title = doc.createParagraph();
        XWPFRun run = title.createRun();
        run.setText("Fig.1 A Natural Scene");
        run.setBold(true);
        title.setAlignment(ParagraphAlignment.CENTER);

        String imgFile = "c:/test/a.jpg";
        FileInputStream is = new FileInputStream(imgFile);
        FileInputStream is2 = new FileInputStream(imgFile);
        run.addBreak();
        run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(400), Units.toEMU(400)); // 200x200 pixels
        run.addPicture(is2, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(400), Units.toEMU(400)); // 200x200 pixels
        is.close();
        is2.close();

        FileOutputStream fos = new FileOutputStream("c:/test/test.docx");
        doc.write(fos);
        fos.close();

    }
    }


        /*public static void main(String[] args)throws Exception {
            //初始化空白正文
//            XWPFDocument document= new XWPFDocument();
            CustomXWPFDocument document=new CustomXWPFDocument();
            //生成word保存地址
            FileOutputStream out = new FileOutputStream(new File("C:\\test\\test.docx"));
            //添加标题
            XWPFParagraph titleParagraph = document.createParagraph();
            //设置段落居中
            titleParagraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun titleParagraphRun = titleParagraph.createRun();
            titleParagraphRun.setText("Java PoI");
//            titleParagraphRun.setColor("000000");
            titleParagraphRun.setFontSize(20);


            //段落
            XWPFParagraph firstParagraph = document.createParagraph();
            XWPFRun run = firstParagraph.createRun();
            run.setText("Java POI 生成word文件。");
//            run.setColor("696969");
            run.setFontSize(16);

            //设置段落背景颜色
            CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
            cTShd.setVal(STShd.CLEAR);
            cTShd.setFill("97FFFF");

            //换行
            XWPFParagraph paragraph1 = document.createParagraph();
            XWPFRun paragraphRun1 = paragraph1.createRun();
            paragraphRun1.setText("\r");

            XWPFParagraph paragraph2 = document.createParagraph();
            document.addPicture(new FileInputStream(new File("C:\\test\\a.jpeg")),5);

            //基本信息表格
            XWPFTable infoTable = document.createTable();
            //去表格边框
            infoTable.getCTTbl().getTblPr().unsetTblBorders();


            //列宽自动分割
            CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
            infoTableWidth.setType(STTblWidth.DXA);
            infoTableWidth.setW(BigInteger.valueOf(9072));


            //表格第一行
            XWPFTableRow infoTableRowOne = infoTable.getRow(0);
            infoTableRowOne.getCell(0).setText("职位");
            infoTableRowOne.addNewTableCell().setText(": Java 开发工程师");

            //表格第二行
            XWPFTableRow infoTableRowTwo = infoTable.createRow();
            infoTableRowTwo.getCell(0).setText("姓名");
            infoTableRowTwo.getCell(1).setText(": seawater");

            //两个表格之间加个换行
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun paragraphRun = paragraph.createRun();
            paragraphRun.setText("\r");

            //创建表格2
            XWPFTable ComTable = document.createTable();

            //列宽自动分割
            CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
            comTableWidth.setType(STTblWidth.DXA);
            comTableWidth.setW(BigInteger.valueOf(9072));

            //表格第一行
            XWPFTableRow comTableRowOne = ComTable.getRow(0);
            comTableRowOne.getCell(0).setText("开始时间");
            comTableRowOne.addNewTableCell().setText("结束时间");
            comTableRowOne.addNewTableCell().setText("公司名称");
            comTableRowOne.addNewTableCell().setText("title");

            //表格第二行
            XWPFTableRow comTableRowTwo = ComTable.createRow();
            comTableRowTwo.getCell(0).setText("2016-09-06");
            comTableRowTwo.getCell(1).setText("至今");
            comTableRowTwo.getCell(2).setText("seawater");
            comTableRowTwo.getCell(3).setText("Java开发工程师");

            CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
            XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);

            //添加页眉-->在wps中不生效
            CTP ctpHeader = CTP.Factory.newInstance();
            CTR ctrHeader = ctpHeader.addNewR();
            CTText ctHeader = ctrHeader.addNewT();
            String headerText = "Java POI create MS word file.";
            ctHeader.setStringValue(headerText);
            XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
            //设置为右对齐
            headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
            XWPFParagraph[] parsHeader = new XWPFParagraph[1];
            parsHeader[0] = headerParagraph;
            policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);


            //添加页脚-->在wps中不生效
            CTP ctpFooter = CTP.Factory.newInstance();
            CTR ctrFooter = ctpFooter.addNewR();
            CTText ctFooter = ctrFooter.addNewT();
            String footerText = "http://blog.csdn.net/zhouseawater";
            ctFooter.setStringValue(footerText);
            XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
            headerParagraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFParagraph[] parsFooter = new XWPFParagraph[1];
            parsFooter[0] = footerParagraph;
            policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);


            document.write(out);
            out.close();
            System.out.println("create_table document written success.");
        }
*/